package com.xray.netdisk.ui;

import com.xray.netdisk.pojo.FileItem;
import com.xray.netdisk.utils.NetworkUtilsV2;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.event.*;
import java.util.List;

/**
 * 客户端窗体
 */
public class ClientForm {

    //表格列名
    public static final Object[] COLUMN_NAMES = {"序号","文件名","长度"};

    public JPanel panel1;
    private JTable tbFileList;
    private JButton btRefresh;
    private JButton btDownload;
    private JButton btUpload;
    private JButton btDelete;
    //文件id
    private Long fileId;
    //用户id
    private Long userId;


    public ClientForm() {
        //点击刷新
        btRefresh.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                refreshFileList(userId);
            }
        });
        //点击上传
        btUpload.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String path = JOptionPane.showInputDialog("文件路径");
                if(path.isEmpty()){
                    JOptionPane.showMessageDialog(null,"请输入上传文件路径");
                    return;
                }
                NetworkUtilsV2.uploadFile(userId,path);
            }
        });
        //点击下载
        btDownload.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(fileId == null){
                    JOptionPane.showMessageDialog(null,"请选择下载的文件");
                    return;
                }
                NetworkUtilsV2.downloadFile(fileId);
                fileId = null;
            }
        });
        //点击删除
        btDelete.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(fileId == null){
                    JOptionPane.showMessageDialog(null,"请选择删除的文件");
                    return;
                }
                NetworkUtilsV2.deleteFile(fileId);
                fileId = null;
            }
        });
        //点击表格
        tbFileList.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                //获得选中的文件id
                int row = tbFileList.getSelectedRow();
                Object valueAt = tbFileList.getModel().getValueAt(row, 0);
                fileId = Long.valueOf(valueAt.toString());
            }
        });
    }

    /**
     * 刷新用户的文件列表
     * @param userId
     */
    public void refreshFileList(Long userId){
        this.userId = userId;
        //查询用户的文件列表
        List<FileItem> fileItems = NetworkUtilsV2.getFileList(userId);
        //转换为二维数组显示到表格里
        Object[][] data = toArray(fileItems);
        TableModel dataModel = new DefaultTableModel(data,COLUMN_NAMES);
        tbFileList.setModel(dataModel);
    }

    /**
     * 集合转二维数组
     * @param items
     * @return
     */
    public Object[][] toArray(List<FileItem> items){
        int size = items.size();
        Object[][] array = new Object[size][3];
        for(int i = 0;i < size;i++){
            FileItem item = items.get(i);
            array[i][0] = item.getId();
            array[i][1] = item.getFilename();
            array[i][2] = item.getLength();
        }
        return array;
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("ClientUI");
        frame.setContentPane(new ClientForm().panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setSize(600,400);
        frame.setVisible(true);
    }
}
